import member from '@/components/selectMember/selectMember.vue';
import {getUrlParam,age} from '@/static/js/filter.js'
export default {
	components:{member},
	data() {
		const currentDate = this.getDate({
			format: true
		})
		return {
			userName: '',
			userId:'',
			dialog: false, //会员弹出框
			showModal: false, 
			showContent: '',
			start: '',
			end: currentDate,
			memberInformation: {},
			userSn: '', //档案编号
			phone: '',
			add: '', //家庭住址
			transferDate: '', //转入时间
			department: '',
			attendingDoctor: '',
			organizationName: '', //机构名称
			initialImpression: '', //初步印象
			medicalHistory: '',
			majorPastHistory: '',
			courseOfTreatment: '',
			referralDoctor: '', //转诊医生
			doctorPhone:'',
			referralInstitution: '', //转诊机构
			referralDate: '', //转诊日期
			id:'',
		}
	},
	onLoad() {
		this.id = getUrlParam('id');
		if(this.id) {
			const _this = this;
			_this.$api.AppHealthReferralRecordsInfo(_this.id).then((res) => {
				if(res.data.returnCode == 200) {				
					_this.userName = res.data.result && res.data.result.userName;
					_this.userId = res.data.result && res.data.result.userId;
					_this.memberDetails(_this.userId)
					_this.userSn = res.data.result && res.data.result.userSn;
					_this.phone = res.data.result && res.data.result.phone;
					_this.add = res.data.result && res.data.result.add;
					_this.transferDate = res.data.result && res.data.result.transferDate;
					_this.department = res.data.result && res.data.result.department;
					_this.attendingDoctor = res.data.result && res.data.result.attendingDoctor;			
					_this.organizationName = res.data.result && res.data.result.organizationName;
					uni.createSelectorQuery().select('#initialImpression').context((c) => {this.editorCtx = c.context; this.editorCtx.setContents({html: res.data.result && res.data.result.initialImpression|| ''})}).exec();
					uni.createSelectorQuery().select('#medicalHistory').context((c) => {this.editorCtx = c.context; this.editorCtx.setContents({html: res.data.result && res.data.result.medicalHistory|| ''})}).exec();
					uni.createSelectorQuery().select('#majorPastHistory').context((c) => {this.editorCtx = c.context; this.editorCtx.setContents({html: res.data.result && res.data.result.majorPastHistory|| ''})}).exec();
					uni.createSelectorQuery().select('#courseOfTreatment').context((c) => {this.editorCtx = c.context; this.editorCtx.setContents({html: res.data.result && res.data.result.courseOfTreatment|| ''})}).exec();
					_this.referralDoctor = res.data.result && res.data.result.referralDoctor;
					_this.doctorPhone = res.data.result && res.data.result.doctorPhone;
					_this.referralInstitution = res.data.result && res.data.result.referralInstitution;
					_this.referralDate = res.data.result && res.data.result.referralDate;
					
				}
			})
		}
	},
	methods: {
		age,
		hidedialog() {
			this.dialog = false
		},
		chooseUser(e){ //选择会员
			this.userName = e.userName;
			this.userId = e.userId;
			this.memberDetails()
		},
		phoneBlur() {
			let phoneCode = /^1[3456789]\d{9}$/
			if(!(phoneCode.test(this.phone)) ) {
				uni.showToast({
					title: '手机号格式不正确',
					icon: 'none'
				})
			} 
		},
		phoneBlur1() {
			let phoneCode = /^1[3456789]\d{9}$/
			if(!(phoneCode.test(this.doctorPhone)) ) {
				uni.showToast({
					title: '手机号格式不正确',
					icon: 'none'
				})
			} 
		},
		// 会员详情接口
		memberDetails() {
			this.$api.findByUserId(this.userId).then((res) => {
				if(res.data.returnCode == 200) {
					this.memberInformation = res.data.result;
					this.memberInformation.age = age('',this.memberInformation.birthday)
				}
				
			})
		},
		showModalBtn(e) {
			const data = {
				userName: this.userName, //会员姓名
				sex: this.memberInformation.sex, 
				userId: this.userId,
				age: this.memberInformation.age,
				userSn: this.userSn, //档案编号
				phone: this.phone,
				add: this.add, //家庭住址
				transferDate: this.transferDate,
				department: this.department, //科室
				attendingDoctor: this.attendingDoctor, //接诊医生
				organizationName: this.organizationName, //机构名称
				initialImpression: this.initialImpression && this.initialImpression.detail.html, //初步印象
				medicalHistory: this.medicalHistory && this.medicalHistory.detail.html, //主要现病史
				majorPastHistory: this.majorPastHistory && this.majorPastHistory.detail.html, //主要既往史
				courseOfTreatment: this.courseOfTreatment && this.courseOfTreatment.detail.html, //治疗经过
				referralDoctor: this.referralDoctor, //转诊医生
				doctorPhone: this.doctorPhone,
				referralInstitution: this.referralInstitution, //转诊机构
				referralDate: this.referralDate, //转诊日期
				status: e,
				id: Number(this.id),
				type: 2, //回转单
			}
			if(!data.id) {
				delete data.id
			}
			
			if(!this.userId) {
				uni.showToast({
					title: '请先选择会员',
					icon: 'none'
				})
				return
			}
			
			if(this.id) {
				if(e == 1) {
					this.$api.AppHealthReferralRecordsUpdate(data).then((res) => {
						if(res.data.returnCode == 200) {
							uni.showToast({
								title: '编辑成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/quickly/referral/list'
								})
							},1000)
						}
					})
				}else {
					this.showModal = true;
					this.showContent = '确认信息无误，签名并发送给用户存入健康档案，发送后该信息将不可修改';
					this.afferent = data
				}
			} else {
				if(e == 1) {
					this.$api.AppHealthReferralRecordsSave(data).then((res) => {
						if(res.data.returnCode == 200) {
							uni.showToast({
								title: '保存成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/quickly/referral/list'
								})
							},1000)
						}else if(res.data.returnCode == 506){
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
					})
				}else {
					this.showModal = true;
					this.showContent = '确认信息无误，签名并发送给用户存入健康档案，发送后该信息将不可修改';
					this.afferent = data
				}
			}
			
		},
		sureconfirm() {
			const url = this.id ? 'AppHealthReferralRecordsUpdate' : 'AppHealthReferralRecordsSave';
			this.$api[url](this.afferent).then((res) => {
				if(res.data.returnCode == 200) {
					uni.showToast({
						title: '签发成功',
						icon:'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/quickly/referral/list'
						})
					},1000)
				}else if(res.data.returnCode == 506){
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
				}
			})
		},
		// 获取当前时间
		getDate(type) {
		   const date = new Date();
		   let year = date.getFullYear();
		   let month = date.getMonth() + 1;
		   let day = date.getDate();
		  
		   if (type === 'start') {
			year = year - 60;
		   } else if (type === 'end') {
			year = year + 2;
		   }
		   month = month > 9 ? month : '0' + month;;
		   day = day > 9 ? day : '0' + day;
		   return `${year}-${month}-${day}`;
		},
		// 日期
		bindDateChange(e) {  //日期选择
			this.transferDate = e.target.value;
		},
		bindDateChange2(e) {
			this.referralDate = e.target.value;
		}
	}
}